package com.lantosec.hmrp.modules.eval.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.modules.eval.entity.EvalPlanList;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.eval.entity.EvalPlan;
import com.lantosec.hmrp.modules.eval.service.IEvalPlanService;
import com.lantosec.hmrp.modules.eval.vo.EvalPlanVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 考评计划 控制器
 *
 * @author AutoCode
 * @since 2020-03-17
 */
@Api(value = "考评计划控制器", tags = "考评计划控制器")
@RestController
@RequestMapping("/eval/plan")
@Log4j2
public class EvalPlanController extends BaseController {
    @Autowired
    private IEvalPlanService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<EvalPlanVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<PageVO<EvalPlanVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<EvalPlanVO> page,
                                         @ApiParam(name ="开始时间" ,value = "startDate") @RequestParam(name = "startDate",required = false) String startDate,
                                         @ApiParam(name ="结束时间" ,value = "endDate") @RequestParam(name = "endDate",required = false) String endDate,
                                         @ApiParam(name ="计划名称" ,value = "evalPlanName") @RequestParam(name = "evalPlanName",required = false) String evalPlanName,
                                         @ApiParam(name ="考评周期" ,value = "evalPlanCycle") @RequestParam(name = "evalPlanCycle",required = false) Integer evalPlanCycle,
                                         @ApiParam(name ="计划类型" ,value = "evalPlanType") @RequestParam(name = "evalPlanType",required = false) Integer evalPlanType,
                                         @ApiParam(name ="考评形式" ,value = "evalMode") @RequestParam(name = "evalMode",required = false) Integer evalMode,
                                         @ApiParam(name ="计划状态" ,value = "state") @RequestParam(name = "state",required = false) Integer state){
        service.queryAll(page,startDate,endDate,evalPlanName,evalPlanCycle,evalPlanType,evalMode,state);
        return Result.ok(page);
    }

    @GetMapping("get/{evalPlanId}")
    @ApiOperation(value = "考评计划详细信息")
    public Result<EvalPlanList> get(@ApiParam(value = "evalPlanId", required = true) @PathVariable(name="evalPlanId") Integer evalPlanId){
        EvalPlanList evalPlanList = service.getEvalPlanDetailed(evalPlanId);
        return Result.ok(evalPlanList);
    }

    @PostMapping("create")
    @ControllerLog(name = "新增考评计划", type = LogType.add)
    @ApiOperation(value = "新增数据")
    public Result<Integer> create(@ApiParam(value = "考评计划封装对象", required = true) @RequestBody @Validated EvalPlanList evalPlanList){
        service.saveAll(evalPlanList);
        return Result.ok();
    }

    @PutMapping("updateAll")
    @ControllerLog(name = "编辑考评计划", type = LogType.update)
    @ApiOperation(value = "编辑方案信息")
    public Result<Integer> updateAll(@ApiParam(value = "考评计划封装对象", required = true) @RequestBody @Validated EvalPlanList evalPlanList){
        service.updateAll(evalPlanList);
        return Result.ok();
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "考评计划VO对象", required = true) @RequestBody EvalPlanVO vo){
        EvalPlan entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }


    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "逻辑删除考评计划", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("getSysDeptPosition")
    @ApiOperation(value = "判断岗位是否是部门的相同岗位")
    public Result<Void> getSysDeptPosition(@ApiParam(value = "部门ID（用，分开）", required = true) @RequestParam(name="deptIds") String[] deptIds,
                                                   @ApiParam(value = "岗位ID", required = true) @RequestParam(name="positionId") String positionId){
        Result result = service.getSysDeptPosition(deptIds,positionId);
        return result;
    }
}
